//
//  fform.swift
//  xzseller
//
//  Created by zhuxietong on 2021/1/27.
//

import Foundation
import RxSwift
extension String{
    
    public func mach(_ reg:String)->Bool{
        do {
            let regObj = try Regex(string: reg)
            switch self {
            case regObj:
                return true
            default:
                return false
            }
        } catch {
            print(error)
            return false
            // error异常的对象
        }
    }
}
extension FForm{
    public class TimeFormat:Format{
        public override var tag:String{
            return "FForm.TimeFormat"
        }
        
        public var displayFormat:TimeFormatType
        public var valueFormat:TimeFormatType
        public var pickerMode:TimePickerMode = .day
        public init(displayF:TimeFormatType,valueF:TimeFormatType,mode:TimePickerMode) {
            displayFormat = displayF
            valueFormat = valueF
            pickerMode = mode
            super.init()
        }
        
        public var date:Date?{
            var date = valueObject as? Date
            if (date == nil){
                let str = "\(valueObject ?? "")"
                if str.len > 2{
                    switch valueFormat {
                    case .dispay:
                        date = Date(str: str, fromat: valueFormat.key)
                    default:
                        date = Date(stamp: valueFormat.key)
                    }
                }
            }
            return date
        }
        public override var formValue: Any{
            if let date_ = valueObject as? Date{
                return date_[valueFormat.key]
            }
            return ""
        }
        
        public override var displayString: String{
            if let date_ = valueObject as? Date{
                return date_[displayFormat.key]
            }
            return ""
        }
    }
    
}

public enum FVerifyResult {
    case success(value:Any)
    case `nil`
    case error(String)
}

public typealias FVerifyMethod = (Any)->FVerifyResult
public enum FVerify {
    case reg(String)
    case method((Any)->FVerifyResult)
}

public enum FVisible {
    case show(Bool)
    case wath(([String:Any])->Bool)
}
public enum FEdit {
    case input
    case select
    case fill
    case reply
    case clear
}

public enum InputType {
    case input
    case select
    case setting
    public var name:String{
        get{
            switch self {
            case .input:
                return "输入"
            case .setting:
                return "设置"
            default:
                return "选择"
            }
        }
    }
}


class FDemo:FForm{
    override func addLayoutRules() {
        let f = FField()
        f.props = [.name("username","姓名"),.verify(.reg("\\S+"))]
        let v = paw()
    }
    
}
open class FForm:MoDisplayNode{
 
    
//    public let bkNode = ASDisplayNode { () -> UIView in
//        return UIControl()
//    }
//
    
    open override func didLayoutRules() {
        
//        let bn = (bkNode.view as? UIControl)
//        bn?.addTarget(self, action: #selector(endEdit), for: .touchDown)
//        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(endEdit)))
//        bk.addTarget(self, action: #selector(endEdit), for: .touchDown)
    }
    
  
    @objc open func endEdit(){
        view.endEditing(true)
    }
    public let disposeBag = DisposeBag()
    public var bags:[Disposable] = []
    public let status:PublishSubject<(String,Any,FEdit,NSMutableDictionary)> = PublishSubject<(String,Any,FEdit,NSMutableDictionary)>()
    public var fields:[FField] = []{
        didSet{
            for old in bags {
                old.dispose()
            }
            bags.removeAll()
            for f in fields {
                let bg = f.status.subscribe(onNext:{[weak self]
                    value in
                    self?.status.onNext(value)
                })
                bags.append(bg)
            }

        }
    }
    
    open func fields(filter group:String?=nil) -> [FField] {

        let nds_ = subnodes ?? []
        var nds = [FField]()
        for n in nds_ {
            if let f = n as? FField{
                nds.append(f)
            }
        }
        return nds
//        return []
    }
    
    public func paw() -> (valid:Bool,values:[String:Any]) {
        var values:[String:Any] = [:]
        for f in fields {
            if f.name.count > 0{
                switch f.verifyResult {
                case .error(let err):
                    err.alert()
                    return (false,values)
                case .nil:
                    f.emptyTip.alert()
                    return (false,values)
                default:
                    
                    values[f.name] = f.value
                }
            }
        }
        return (true,values)
    }
    
}


